package com.basic.common.json;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.basic.common.result.WebResult;
import com.github.pagehelper.PageInfo;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * json工具类
 *
 * @author
 * @version v_1.0.1
 * @since 2022/07/18 13:54
 */
public class JsonDsUtil {

    /**
     * @param
     * @explain json字符串转bean
     * @author
     * @version v_1.0.1
     * @since 2022/07/18 13:54
     */
    public static <T> T jsonStrToBean(String jsonStr, Class<T> beanClass) {
        if (StringUtils.isNotBlank(jsonStr) && StringUtils.isNotBlank(jsonStr.trim())) {
            return JSONObject.parseObject(jsonStr, beanClass);
        }
        return null;
    }

    /**
     * jsonStr转bean（待测）
     *
     * @param jsonStr
     * @return T
     * @author
     * @since 2022/07/18 13:54
     */
    public static <T> T jsonStrToCoBean(String jsonStr) {
        if (StringUtils.isNotBlank(jsonStr) && StringUtils.isNotBlank(jsonStr.trim())) {
            return JSONObject.parseObject(jsonStr, new TypeReference<T>() {
            });
        }
        return null;
    }

    /**
     * json字符串转list
     *
     * @param jsonStr
     * @param beanClass
     * @param <T>
     * @return
     */
    public static <T> List<T> jsonStrToBeanList(String jsonStr, Class<T> beanClass) {
        if (StringUtils.isNotBlank(jsonStr) && StringUtils.isNotBlank(jsonStr.trim())) {
            return JSONObject.parseArray(jsonStr, beanClass);
        }
        return null;
    }

    /**
     * json转map
     *
     * @param jsonStr
     * @return java.util.Map<java.lang.String,java.lang.Object>
     * @author
     * @since 2022/07/18 13:54
     */
    public static Map<String, Object> jsonStrToMap(String jsonStr) {
        if (StringUtils.isNotBlank(jsonStr) && StringUtils.isNotBlank(jsonStr.trim())) {
            return JSONObject.parseObject(jsonStr, Map.class);
        }
        return null;
    }

    /**
     * @param
     * @explain json字符串转JSONObject
     * @author
     * @version v_1.0.1
     * @since 2022/07/18 13:54
     */

    public static JSONObject jsonStrToJSONObject(String jsonStr) {
        if (StringUtils.isNotBlank(jsonStr) && StringUtils.isNotBlank(jsonStr.trim())) {
            return JSONObject.parseObject(jsonStr);
        }
        return null;
    }


    /**
     * javaBean转换为JSON字符串
     * 支持List<T>或T
     *
     * @param bean
     * @return
     */
    public static <T> String beanToJSONStr(T bean) {
        if (null != bean) {
            return JSONObject.toJSONString(bean);
        }
        return null;
    }

    /**
     * @param
     * @explain object转json字符串
     * @author
     * @version v_1.0.1
     * @since 2022/07/18 13:54
     */
    public static String objToJSONStr(Object obj) {
        if (null != obj) {
            return JSONObject.toJSONString(obj);
        }
        return null;
    }

    /**
     * object转json字符串(显示空值)
     *
     * @param obj
     * @return java.lang.String
     * @author
     * @since 2022/07/18 13:54
     */
    public static String objToJSONStrNoNull(Object obj) {
        if (null != obj) {
            return JSONObject.toJSONString(obj, SerializerFeature.WriteMapNullValue);
        }
        return null;
    }

    /**
     * obj转bean
     *
     * @param obj
     * @return T
     * @author
     * @since 2022/07/18 13:54
     */
    public static <T> T objToCoBean(Object obj, Class<T> beanClass) {
        if (obj != null) {
            String s = objToJSONStr(obj);
            return jsonStrToBean(s, beanClass);
        }
        return null;
    }

    /**
     * obj转bean集合
     *
     * @param obj
     * @param beanClass
     * @return java.util.List<T>
     * @author
     * @since 2022/07/18 13:54
     */
    public static <T> List<T> objToBeanList(Object obj, Class<T> beanClass) {
        if (obj != null) {
            String s = JsonDsUtil.beanToJSONStr(obj);
            return jsonStrToBeanList(s, beanClass);
        }
        return null;
    }

    /**
     * pageInfoData转beanlist
     *
     * @param object
     * @return java.util.List<T>
     * @author
     * @since 2022/07/18 13:54
     */
    public static <T> List<T> pageInfoListToCoBeanList(Object object, Class<T> beanClass) {
        List<T> list1 = new ArrayList<>();
        if (object != null) {
            String s1 = JsonDsUtil.objToJSONStr(object);
            PageInfo pageInfo = JsonDsUtil.jsonStrToBean(s1, PageInfo.class);
            if (pageInfo != null) {
                List list = pageInfo.getList();
                if (CollectionUtils.isNotEmpty(list)) {
                    List<T> ts = objToBeanList(list, beanClass);
                    return ts;
                }
            }
        }
        return list1;
    }

//    /**
//     * PageInfo-list转换bean
//     *
//     * @param pageInfo
//     * @param beanClass
//     * @return java.util.List<T>
//     * @author
//     * @since 2019/11/21 20:13
//     */
//    public static <T> PageInfo<T> pageInfoListCopierBeanList(PageInfo pageInfo, Class<T> beanClass) {
//        if (pageInfo != null) {
//            List list = pageInfo.getList();
//            if (CollectionUtils.isNotEmpty(list)) {
//                List<T> ts1 = BeanCopierDsUtil.copyPropertiesOfList(list, beanClass);
//                pageInfo.setList(ts1);
//                return pageInfo;
//            }
//        }
//        return pageInfo;
//    }

    public static void main(String[] args) {
        String s = "{\"code\":\"0\",\"data\":true,\"exception\":\"SUCCESS\",\"msg\":\"成功\"}";
        System.out.println(jsonStrToBean(s, WebResult.class));

    }


}
